/** 
 * @author Riccardo Manfrin [namesurname at gmail dot com]
 * 
 * @brief  Main transmitter/receiver file
 * 
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>   /* handling exiting signal */
#include "data_sock.h"
#include "fileparser.h"
#include "debug.h"
#include "defines.h"

static void leave();

int main(int argc, char *argv[]){
	char *command;
	FILE *cmds = NULL;
	int tx=0,rx=0;
	int i = 1;
	ds_init_data();
	(void) signal(SIGINT, leave);
	while(argv[i]!=NULL)
	{
		if(memcmp(argv[i],"-v", 2)==0) {dbg_verbose(atoi(argv[++i]));continue;}
		if(memcmp(argv[i],"-t", 2)==0) {cmds= fopen(argv[++i], "r");tx=1;}
		if(memcmp(argv[i],"-r", 2)==0) {cmds= fopen(argv[++i], "r");rx=1;}
		if(cmds<0) return -1;
		i++;
	}
	if(cmds==NULL)
	{
		return -1;
	}
	
	
	command = malloc(CMD_BUFF_SZ);
	memset(command,0,CMD_BUFF_SZ);
	while(fgets(command,CMD_BUFF_SZ,cmds)){
		if(tx)parser_tx_file(command);
		if(rx)parser_rx_file(command);
		memset(command,0,CMD_BUFF_SZ);
	}

	fclose(cmds);
	printf("Done!\n");
	return 0;
}

static void leave(){
  printf("Forced quit! ByeBye\n");
  exit(0);
}
